using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.IO;
using System.Globalization;

using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	/// <summary>
	/// Summary description for ShopListForm.
	/// </summary>
	public class ShopListForm : System.Windows.Forms.Form, IListViewForm
	{
		private System.Windows.Forms.ListView listView;
		private System.Windows.Forms.ColumnHeader columnHeader_Key;
		private System.Windows.Forms.ColumnHeader columnHeader_Name;
		private System.Windows.Forms.ColumnHeader columnHeader_Amount;
		private System.Windows.Forms.ColumnHeader columnHeader_Purchased;
		private System.Windows.Forms.ImageList imageList;
		public System.Windows.Forms.ToolBarButton toolBarButton_Plus;
		public System.Windows.Forms.ToolBarButton toolBarButton_Minus;
		private System.Windows.Forms.ToolBar toolBar;
		private System.Windows.Forms.Label lbl_Summary;
		private System.Windows.Forms.Button btn_Cancel;
		private System.Windows.Forms.CheckBox chk_HidePurchased;
		private System.Windows.Forms.ContextMenu contextMenu;
		private System.Windows.Forms.MenuItem ctxMenu_ReturnOne;
		private System.Windows.Forms.MenuItem ctxMenu_TakeAll;
		private System.Windows.Forms.MenuItem ctxMenu_TakeOne;
		private System.Windows.Forms.Label lbl_TotalPrice;

		private	Categories				categories;
		public System.Windows.Forms.ToolBarButton toolBarButton_Notes;
		public System.Windows.Forms.ToolBarButton toolBarButton_Delete;
		public System.Windows.Forms.ToolBarButton toolBarButton_Export;
		private ShoppingListFormManager listFormManager;

		

		#region Constructors
	
		public ShopListForm(Shop shop)
		{
			InitializeComponent();

			// A workaround WM2003 SE Toolbar bug
			toolBar.ImageList = imageList;
			toolBar.Buttons[0].ImageIndex = 0;
			toolBar.Buttons[1].ImageIndex = 1;
			toolBar.Buttons[2].ImageIndex = 2;
			toolBar.Buttons[3].ImageIndex = 3;
			toolBar.Buttons[4].ImageIndex = 4;

			// Create categories that can be shopped in this shop
			categories = shop.CreateShoppingList();

			listFormManager = new ShoppingListFormManager(this, listView, 
								  categories, shop);

			listFormManager.ShopListChanged+=new EventHandler(listFormManager_ShopListChanged);

		}
		#endregion

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ShopListForm));
			this.toolBar = new System.Windows.Forms.ToolBar();
			this.toolBarButton_Plus = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Minus = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Notes = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Delete = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Export = new System.Windows.Forms.ToolBarButton();
			this.imageList = new System.Windows.Forms.ImageList();
			this.listView = new System.Windows.Forms.ListView();
			this.columnHeader_Key = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Name = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Amount = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_Purchased = new System.Windows.Forms.ColumnHeader();
			this.contextMenu = new System.Windows.Forms.ContextMenu();
			this.ctxMenu_TakeOne = new System.Windows.Forms.MenuItem();
			this.ctxMenu_ReturnOne = new System.Windows.Forms.MenuItem();
			this.ctxMenu_TakeAll = new System.Windows.Forms.MenuItem();
			this.lbl_Summary = new System.Windows.Forms.Label();
			this.btn_Cancel = new System.Windows.Forms.Button();
			this.chk_HidePurchased = new System.Windows.Forms.CheckBox();
			this.lbl_TotalPrice = new System.Windows.Forms.Label();
			// 
			// toolBar
			// 
			this.toolBar.Buttons.Add(this.toolBarButton_Plus);
			this.toolBar.Buttons.Add(this.toolBarButton_Minus);
			this.toolBar.Buttons.Add(this.toolBarButton_Notes);
			this.toolBar.Buttons.Add(this.toolBarButton_Delete);
			this.toolBar.Buttons.Add(this.toolBarButton_Export);
			this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
			// 
			// toolBarButton_Plus
			// 
			this.toolBarButton_Plus.Enabled = ((bool)(resources.GetObject("toolBarButton_Plus.Enabled")));
			this.toolBarButton_Plus.ImageIndex = ((int)(resources.GetObject("toolBarButton_Plus.ImageIndex")));
			this.toolBarButton_Plus.Visible = ((bool)(resources.GetObject("toolBarButton_Plus.Visible")));
			// 
			// toolBarButton_Minus
			// 
			this.toolBarButton_Minus.Enabled = ((bool)(resources.GetObject("toolBarButton_Minus.Enabled")));
			this.toolBarButton_Minus.ImageIndex = ((int)(resources.GetObject("toolBarButton_Minus.ImageIndex")));
			this.toolBarButton_Minus.Visible = ((bool)(resources.GetObject("toolBarButton_Minus.Visible")));
			// 
			// toolBarButton_Notes
			// 
			this.toolBarButton_Notes.Enabled = ((bool)(resources.GetObject("toolBarButton_Notes.Enabled")));
			this.toolBarButton_Notes.ImageIndex = ((int)(resources.GetObject("toolBarButton_Notes.ImageIndex")));
			this.toolBarButton_Notes.Visible = ((bool)(resources.GetObject("toolBarButton_Notes.Visible")));
			// 
			// toolBarButton_Delete
			// 
			this.toolBarButton_Delete.Enabled = ((bool)(resources.GetObject("toolBarButton_Delete.Enabled")));
			this.toolBarButton_Delete.ImageIndex = ((int)(resources.GetObject("toolBarButton_Delete.ImageIndex")));
			this.toolBarButton_Delete.Visible = ((bool)(resources.GetObject("toolBarButton_Delete.Visible")));
			// 
			// toolBarButton_Export
			// 
			this.toolBarButton_Export.Enabled = ((bool)(resources.GetObject("toolBarButton_Export.Enabled")));
			this.toolBarButton_Export.ImageIndex = ((int)(resources.GetObject("toolBarButton_Export.ImageIndex")));
			this.toolBarButton_Export.Visible = ((bool)(resources.GetObject("toolBarButton_Export.Visible")));
			// 
			// imageList
			// 
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource3"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource4"))));
			this.imageList.ImageSize = ((System.Drawing.Size)(resources.GetObject("imageList.ImageSize")));
			// 
			// listView
			// 
			this.listView.Columns.Add(this.columnHeader_Key);
			this.listView.Columns.Add(this.columnHeader_Name);
			this.listView.Columns.Add(this.columnHeader_Amount);
			this.listView.Columns.Add(this.columnHeader_Purchased);
			this.listView.ContextMenu = this.contextMenu;
			this.listView.Enabled = ((bool)(resources.GetObject("listView.Enabled")));
			this.listView.FullRowSelect = true;
			this.listView.Location = ((System.Drawing.Point)(resources.GetObject("listView.Location")));
			this.listView.Size = ((System.Drawing.Size)(resources.GetObject("listView.Size")));
			this.listView.Text = resources.GetString("listView.Text");
			this.listView.View = System.Windows.Forms.View.Details;
			this.listView.Visible = ((bool)(resources.GetObject("listView.Visible")));
			this.listView.ItemActivate += new System.EventHandler(this.listView_ItemActivate);
			this.listView.SelectedIndexChanged += new System.EventHandler(this.listView_SelectedIndexChanged);
			// 
			// columnHeader_Key
			// 
			this.columnHeader_Key.Text = resources.GetString("columnHeader_Key.Text");
			this.columnHeader_Key.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Key.TextAlign")));
			this.columnHeader_Key.Width = ((int)(resources.GetObject("columnHeader_Key.Width")));
			// 
			// columnHeader_Name
			// 
			this.columnHeader_Name.Text = resources.GetString("columnHeader_Name.Text");
			this.columnHeader_Name.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Name.TextAlign")));
			this.columnHeader_Name.Width = ((int)(resources.GetObject("columnHeader_Name.Width")));
			// 
			// columnHeader_Amount
			// 
			this.columnHeader_Amount.Text = resources.GetString("columnHeader_Amount.Text");
			this.columnHeader_Amount.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Amount.TextAlign")));
			this.columnHeader_Amount.Width = ((int)(resources.GetObject("columnHeader_Amount.Width")));
			// 
			// columnHeader_Purchased
			// 
			this.columnHeader_Purchased.Text = resources.GetString("columnHeader_Purchased.Text");
			this.columnHeader_Purchased.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Purchased.TextAlign")));
			this.columnHeader_Purchased.Width = ((int)(resources.GetObject("columnHeader_Purchased.Width")));
			// 
			// contextMenu
			// 
			this.contextMenu.MenuItems.Add(this.ctxMenu_TakeOne);
			this.contextMenu.MenuItems.Add(this.ctxMenu_ReturnOne);
			this.contextMenu.MenuItems.Add(this.ctxMenu_TakeAll);
			// 
			// ctxMenu_TakeOne
			// 
			this.ctxMenu_TakeOne.Enabled = ((bool)(resources.GetObject("ctxMenu_TakeOne.Enabled")));
			this.ctxMenu_TakeOne.Text = resources.GetString("ctxMenu_TakeOne.Text");
			this.ctxMenu_TakeOne.Click += new System.EventHandler(this.ctxMenu_TakeOne_Click);
			// 
			// ctxMenu_ReturnOne
			// 
			this.ctxMenu_ReturnOne.Enabled = ((bool)(resources.GetObject("ctxMenu_ReturnOne.Enabled")));
			this.ctxMenu_ReturnOne.Text = resources.GetString("ctxMenu_ReturnOne.Text");
			this.ctxMenu_ReturnOne.Click += new System.EventHandler(this.ctxMenu_ReturnOne_Click);
			// 
			// ctxMenu_TakeAll
			// 
			this.ctxMenu_TakeAll.Enabled = ((bool)(resources.GetObject("ctxMenu_TakeAll.Enabled")));
			this.ctxMenu_TakeAll.Text = resources.GetString("ctxMenu_TakeAll.Text");
			this.ctxMenu_TakeAll.Click += new System.EventHandler(this.ctxMenu_TakeAll_Click);
			// 
			// lbl_Summary
			// 
			this.lbl_Summary.Enabled = ((bool)(resources.GetObject("lbl_Summary.Enabled")));
			this.lbl_Summary.Font = ((System.Drawing.Font)(resources.GetObject("lbl_Summary.Font")));
			this.lbl_Summary.Location = ((System.Drawing.Point)(resources.GetObject("lbl_Summary.Location")));
			this.lbl_Summary.Size = ((System.Drawing.Size)(resources.GetObject("lbl_Summary.Size")));
			this.lbl_Summary.Text = resources.GetString("lbl_Summary.Text");
			this.lbl_Summary.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lbl_Summary.TextAlign")));
			this.lbl_Summary.Visible = ((bool)(resources.GetObject("lbl_Summary.Visible")));
			// 
			// btn_Cancel
			// 
			this.btn_Cancel.Enabled = ((bool)(resources.GetObject("btn_Cancel.Enabled")));
			this.btn_Cancel.Font = ((System.Drawing.Font)(resources.GetObject("btn_Cancel.Font")));
			this.btn_Cancel.Location = ((System.Drawing.Point)(resources.GetObject("btn_Cancel.Location")));
			this.btn_Cancel.Size = ((System.Drawing.Size)(resources.GetObject("btn_Cancel.Size")));
			this.btn_Cancel.Text = resources.GetString("btn_Cancel.Text");
			this.btn_Cancel.Visible = ((bool)(resources.GetObject("btn_Cancel.Visible")));
			this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
			// 
			// chk_HidePurchased
			// 
			this.chk_HidePurchased.Checked = true;
			this.chk_HidePurchased.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chk_HidePurchased.Enabled = ((bool)(resources.GetObject("chk_HidePurchased.Enabled")));
			this.chk_HidePurchased.Font = ((System.Drawing.Font)(resources.GetObject("chk_HidePurchased.Font")));
			this.chk_HidePurchased.Location = ((System.Drawing.Point)(resources.GetObject("chk_HidePurchased.Location")));
			this.chk_HidePurchased.Size = ((System.Drawing.Size)(resources.GetObject("chk_HidePurchased.Size")));
			this.chk_HidePurchased.Text = resources.GetString("chk_HidePurchased.Text");
			this.chk_HidePurchased.Visible = ((bool)(resources.GetObject("chk_HidePurchased.Visible")));
			this.chk_HidePurchased.CheckStateChanged += new System.EventHandler(this.chk_HidePurchased_CheckStateChanged);
			// 
			// lbl_TotalPrice
			// 
			this.lbl_TotalPrice.Enabled = ((bool)(resources.GetObject("lbl_TotalPrice.Enabled")));
			this.lbl_TotalPrice.Font = ((System.Drawing.Font)(resources.GetObject("lbl_TotalPrice.Font")));
			this.lbl_TotalPrice.Location = ((System.Drawing.Point)(resources.GetObject("lbl_TotalPrice.Location")));
			this.lbl_TotalPrice.Size = ((System.Drawing.Size)(resources.GetObject("lbl_TotalPrice.Size")));
			this.lbl_TotalPrice.Text = resources.GetString("lbl_TotalPrice.Text");
			this.lbl_TotalPrice.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lbl_TotalPrice.TextAlign")));
			this.lbl_TotalPrice.Visible = ((bool)(resources.GetObject("lbl_TotalPrice.Visible")));
			// 
			// ShopListForm
			// 
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.Controls.Add(this.lbl_TotalPrice);
			this.Controls.Add(this.chk_HidePurchased);
			this.Controls.Add(this.btn_Cancel);
			this.Controls.Add(this.lbl_Summary);
			this.Controls.Add(this.listView);
			this.Controls.Add(this.toolBar);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.Text = resources.GetString("$this.Text");
			this.Visible = ((bool)(resources.GetObject("$this.Visible")));
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ShopListForm_Closing);
			this.Load += new System.EventHandler(this.ShopListForm_Load);

		}
		#endregion

		#region IListViewForm Members

		public ToolBarButton DeleteButton
		{
			get
			{
				// TODO:  Add ShopListForm.DeleteButton getter implementation
				return null;
			}
		}

		public ToolBarButton EditButton
		{
			get
			{
				// TODO:  Add ShopListForm.EditButton getter implementation
				return null;
			}
		}

		public ToolBarButton NewButton
		{
			get
			{
				// TODO:  Add ShopListForm.NewButton getter implementation
				return null;
			}
		}

		public ToolBarButton UpButton
		{
			get
			{
				// TODO:  Add ShopListForm.UpButton getter implementation
				return null;
			}
		}

		public ToolBarButton DownButton
		{
			get
			{
				// TODO:  Add ShopListForm.DownButton getter implementation
				return null;
			}
		}

		public IItemPropertiesForm CreatePropertiesForm(ItemBase item)
		{
			// TODO:  Add ShopListForm.CreatePropertiesForm implementation
			return null;
		}

		#endregion

		#region Form Event Handlers
		private void ShopListForm_Load(object sender, System.EventArgs e)
		{
			listFormManager.HidePurchased = chk_HidePurchased.Checked;
			listFormManager.FillListView();
			UpdateSummaryText();
		}

		private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			listFormManager.ToolbarEventsHandler(e);
		}

		private void listView_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			listFormManager.EnableDisableItems();
		}

		private void ShopListForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (DialogResult == DialogResult.Cancel) 
			{
				if (!listFormManager.EmptyList &&  MessageBox.Show(LangResources.Instance.Resources.GetString("cancelShopList"), 
					LangResources.Instance.Resources.GetString("applName"), MessageBoxButtons.YesNo,
					MessageBoxIcon.Question,
					MessageBoxDefaultButton.Button2) != DialogResult.Yes) 
				{
					e.Cancel = true;
				} 
				else 
				{
					listFormManager.ReturnAll();
				}
			} 
			else 
			{
				if (MessageBox.Show(LangResources.Instance.Resources.GetString("approveShopList"),
					LangResources.Instance.Resources.GetString("applName"), MessageBoxButtons.YesNo,
					MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) != DialogResult.Yes) 
				{
					e.Cancel = true;
				} 
				else 
				{
					// Append to the CSV file 
					exportToFile();
					listFormManager.UpdateAmounts();
					// Save the database
					Database.Instance.Save();
				}
			}
		}

		private void btn_Cancel_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		private void listView_ItemActivate(object sender, System.EventArgs e)
		{
			listFormManager.HandleItemActivateEvent();
		}

		private void chk_HidePurchased_CheckStateChanged(object sender, System.EventArgs e)
		{
			listFormManager.HidePurchased = chk_HidePurchased.Checked;
			listFormManager.FillListView();
		}

		private void ctxMenu_TakeOne_Click(object sender, System.EventArgs e)
		{
			listFormManager.ToolbarEventsHandler(new ToolBarButtonClickEventArgs(toolBarButton_Plus));
		}

		private void ctxMenu_ReturnOne_Click(object sender, System.EventArgs e)
		{
			listFormManager.ToolbarEventsHandler(new ToolBarButtonClickEventArgs(toolBarButton_Minus));
		}

		private void ctxMenu_TakeAll_Click(object sender, System.EventArgs e)
		{
			listFormManager.HandleItemActivateEvent();		
		}

		#endregion

		#region Private Event Handlers
		private void listFormManager_ShopListChanged(object sender, EventArgs e)
		{
			UpdateSummaryText();
		}
		#endregion

		#region Utility methods
		private void exportToFile()
		{
			string message = null;
			try 
			{
				Statistics.Instance.ExportListToCSV(categories);
			}
			catch(IOException) 
			{
				message = LangResources.Instance.Resources.GetString("exportErr") + " " + Database.Instance.ExportCSVFile;
				MessageBox.Show(message, LangResources.Instance.Resources.GetString("applName"),
					MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
			}
		}

		private void UpdateSummaryText()
		{
			string summaryText = listFormManager.CompletedCount + " " + LangResources.Instance.Resources.GetString("summaryText")
				+ " " + listFormManager.Count;

			lbl_Summary.Text = summaryText;
			lbl_TotalPrice.Text = string.Format("{0:c}", listFormManager.CompletePrice);
		}
		#endregion
	}
}
